<?php
class CityModel extends BaseModel {
	/**
	 * <h2>日志对象</h2>
	 *
	 * @var Logger
	 */
	private $_logger;
	public function __construct() {
		parent::__construct ();
		Logger::configure ( ROOT_PATH . 'config/log_config.xml' );
		$this->_logger = Logger::getLogger ( __CLASS__ );
	}
	
	/**
	 * <h2>获取所有城市</h2>
	 * <p>城市实体存储在map中，然后由map构成一个List，返回</p>
	 *
	 * @return CLIST(CMAP(id,name,pid))
	 */
	public function getAll() {
		$list = new CLIST ();
		$conn = DBHelper::getDB ( false );
		$query = 'SELECT id, name, pid FROM cities';
		$resultSet = $conn->select ( $query );
		for($i = 0; $i < count ( $resultSet ); ++ $i) {
			$map = new CMAP ( $resultSet [$i] );
			$list->append ( $map );
		}
		return $list;
	}
	
	/**
	 * <h2>获取一个省份下所有的城市</h2>
	 * <p>城市存在map中，返回存储map的list</p>
	 *
	 * @return CLIST(CMAP(id,name))
	 */
	public function getCityByProvinceId($pid) {
		$list = new CLIST ();
		$query = 'SELECT id, name FROM cities WHERE pid=:pid';
		$param ['pid'] = $pid;
		$map [] = array (
				'pid',
				'int' 
		);
		$resultSet = $this->_conn->select ( $query, $param, $map );
		for($i = 0; $i < count ( $resultSet ); ++ $i) {
			$map = new CMAP ( $resultSet [$i] );
			$list->append ( $map );
		}
		return $list;
	}
	
	/**
	 * <h2>根据城市的ID获取城市的详细记录</h2>
	 * <p>记录保存在一个map中，如果没有找到，返回null</p>
	 *
	 * @return CMAP(id,name,zipcode,pid)
	 */
	public function getDetailById($id) {
		$query = 'SELECT id, name, zipcode, pid FROM cities WHERE id=:id LIMIT 1';
		$param ['id'] = $id;
		$map [] = array (
				'id',
				'int' 
		);
		$resultSet = $this->_conn->select ( $query, $param, $map );
		if (count ( $resultSet )) {
			return new CMAP ( $resultSet [0] );
		}
		return null;
	}
}